<?php
function getRecentCommentNotifies($blogid, $category, $page, $count) {
	global $database;
	$sql = "SELECT 
				c.*, 
				csiteinfo.title AS siteTitle,
				csiteinfo.name AS nickname,
				csiteinfo.url AS siteUrl,
				csiteinfo.modified AS siteModified
			FROM 
				{$database['prefix']}CommentsNotified c
			LEFT JOIN 
					{$database['prefix']}CommentsNotifiedSiteInfo csiteinfo ON c.siteId = csiteinfo.id  
			WHERE c.blogid = $blogid AND (c.parent is null)";
	$sql .= ' ORDER BY c.modified DESC';
	return fetchWithPaging($sql, $page, $count);
}

function getCommentCommentsNotified_CT($parent) {
	global $database, $blogid;
	$comments = array();
	$authorized = doesHaveOwnership();
	$sql = "SELECT
				c.*, 
				csiteinfo.title AS siteTitle,
				csiteinfo.name AS nickname,
				csiteinfo.url AS siteUrl,
				csiteinfo.modified AS siteModified
			FROM 
					{$database['prefix']}CommentsNotified c 
				LEFT JOIN 
					{$database['prefix']}CommentsNotifiedSiteInfo csiteinfo ON c.siteId = csiteinfo.id  
			WHERE c.blogid = $blogid AND c.parent=$parent";
	$sql .= ' ORDER BY c.written ASC';
	if ($result = DBQuery::query($sql)) {
		while ($comment = mysql_fetch_array($result)) {
			if (($comment['secret'] == 1) && !$authorized) {
				$comment['name'] = '';
				$comment['homepage'] = '';
				$comment['comment'] = _t('관리자만 볼 수 있는 댓글입니다.');
			}
			array_push($comments, $comment);
		}
	}
	return $comments;
}

function getRecentCommentNotifiesView($template) {
	global $blogid, $blogURL, $skinSetting, $configVal;
	requireComponent("Eolin.PHP.Core");
	requireComponent("Textcube.Function.misc");
	$data = misc::fetchConfigVal($configVal);
	$limitLine = ($data['notifiesList'])?$data['notifiesList']:$skinSetting['commentsOnRecent'];
	list($comments, $paging) = getRecentCommentNotifies($blogid,0,null,$limitLine);
	$mergedComments = array();
	setUserSetting('lastVisitNotifiedPage', time());
	for ($i = 0; $i < count($comments); $i++) {
		array_push($mergedComments, $comments[$i]);
		$result = getCommentCommentsNotified_CT($comments[$i]['id']);
		for ($j = 0; $j < count($result); $j++) {
			array_push($mergedComments, $result[$j]);
		}
	}

	$nameNumber = array();
	ob_start();
	for ($i=0; $i<sizeof($mergedComments); $i++) {
		$comment = $mergedComments[$i];
		if($comment['parent']){
			$view = "$template";
			misc::dress('rctrp_rep_link', $comment['url'], $view);
			misc::dress('rctrp_rep_desc', htmlspecialchars(UTF8::lessenAsEm($comment['comment'], 30)), $view);
			misc::dress('rctrp_rep_time', Timestamp::format3($comment['written']), $view);
			misc::dress('rctrp_rep_name', htmlspecialchars(UTF8::lessenAsEm($comment['name'],10)), $view);
			print $view;
		}
	}
	$view = ob_get_contents();
	ob_end_clean();
	return $view;
}

function CT_RecentCommentNotifies($target) {
	global $blogid;

	$target .= '<ol>'.CRLF;
	$target .= getRecentCommentNotifiesView('<li><span class="date" style="display: block; font-family: Verdana, 돋움, Dotum, Tahoma, \'Lucida Grande\', sans-serif; font-size: 0.9em;">[##_rctrp_rep_time_##]</span> <a href="[##_rctrp_rep_link_##]" onclick="window.open(this.href); return false;">[##_rctrp_rep_desc_##]</a> <span class="name" style="color: #ABABAB;">[##_rctrp_rep_name_##]</span></li>'.CRLF);
	$target .= '</ol>'.CRLF;

	return $target;
}

function CT_RecentCommentNotifies_DataSet($DATA){
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($DATA);
	return true;
}
?>